//
//  ViewController.m
//  本地推送通知
//
//  Created by Jones on 12/27/16.
//  Copyright © 2016 Jones. All rights reserved.
//
/* 1 属性类型 NSData NSDate：time 2 使用场景
 ／／ 1 time 2 设置显示时间
@property(nullable, nonatomic,copy) NSDate *fireDate;
 时区
@property(nullable, nonatomic,copy) NSTimeZone *timeZone;
@property(nonatomic) NSCalendarUnit repeatInterval;／／ 重复周期
 重复日期
@property(nullable, nonatomic,copy) NSCalendar *repeatCalendar;
 
@property(nullable, nonatomic,copy) NSString *alertBody;
@property(nonatomic) BOOL hasAction;
 
@property(nullable, nonatomic,copy) NSString *alertAction;
 ／／ 图片
@property(nullable, nonatomic,copy) NSString *alertLaunchImage;

@property(nullable, nonatomic,copy) NSString *alertTitle
 铃声
@property(nullable, nonatomic,copy) NSString *soundName;
 数字
@property(nonatomic) NSInteger applicationIconBadgeNumber;
 推送信息
@property(nullable, nonatomic,copy) NSDictionary *userInfo;

@property (nullable, nonatomic, copy) NSString *category NS_AVAILABLE_IOS(8_0);
 */
#import "NotificationVC.h"

@interface NotificationVC ()

//- (IBAction)btAction:(id)sender;
@end

@implementation NotificationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *registerBtn = [[UIButton alloc]initWithFrame:CGRectMake(150,300, 150, 30)];    registerBtn.backgroundColor = [UIColor lightGrayColor];
    [registerBtn setTitle:@"发送本地通知" forState:UIControlStateNormal];
    [registerBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [registerBtn addTarget:self action:@selector(localNotifAction) forControlEvents:UIControlEventTouchUpInside];
    registerBtn.layer.borderWidth = 1.0;
    registerBtn.layer.cornerRadius = 3.0;
    [self.view addSubview:registerBtn];
    
    // category: 1 实例化
    UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc]init];
    category.identifier = @"testmessage";
    // action : 2 动作
    UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc]init];
    action1.identifier = @"testmessage.lp";
    action1.title = @"老婆大人";
    action1.activationMode = UIUserNotificationActivationModeForeground;
    
    UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc]init];
    action2.identifier = @"testmessage.leader";
    action2.title = @"经理大人";
    action2.activationMode = UIUserNotificationActivationModeForeground;
    // 3 add
    [category setActions:@[action1,action2] forContext:UIUserNotificationActionContextDefault];
    
    NSSet *set = [NSSet setWithObjects:category, nil];
//     NSNotification 1 注册 2 响应方法 3 发送
//    1 注册  | 当前几个属性同时生效 位 ： 或
    
    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge |UIUserNotificationTypeSound |UIUserNotificationTypeAlert  categories:set];
    [[UIApplication sharedApplication]registerUserNotificationSettings:setting];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// app 前台 推送通知不显示
//- (IBAction)btAction:(id)sender {
-(void)localNotifAction{
    NSLog(@"准备发送通知");
//    3 发送 1 实例 2 属性 3 添加
    UILocalNotification *localNotif = [[UILocalNotification alloc]init];
    
    localNotif.alertBody = @"you have a new message";
    localNotif.alertAction = @"a new message";
    localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];// 5 秒之后
    localNotif.applicationIconBadgeNumber = 8;
    localNotif.category = @"message";
    localNotif.userInfo = @{@"message":@"lp",@"something":@"go home"};
    localNotif.category = @"testmessage";
    [[UIApplication sharedApplication]scheduleLocalNotification:localNotif];
}
@end
